<?php
namespace app\console\controller;

use app\console\model\Promotion as PromotionAlias;
use think\Loader;
use think\Request;
use think\Url;
use think\Controller;

class Promotion extends Controller
{
    /*
     * 促销活动列表
     */
    public function index()
    {
        // 获取参数
        $inquire = Request::instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        
        // 读取促销活动
        $data = PromotionAlias::where($condition)->order(['sort' => 'desc', 'id' => 'desc'])->paginate(10, false, ['query' => $inquire]);
        return $this->fetch('index', [
            'mate_title' => '热点活动列表',
            'data'       => $data,
        ]);
    }


    /*
     * 促销活动添加
     */
    public function create()
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('Promotion');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new PromotionAlias())->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('Promotion/index'));
            }

            fail('操作失败');

        }

        return $this->fetch('create', [
            "mate_title" => '促销活动添加',
        ]);

    }

    /*
     * 促销活动删除
     */
    public function destroy($id)
    {

        if (PromotionAlias::destroy($id)) {
            win('删除成功', Url::build('Promotion/index'));
        }

        fail('删除失败');

    }

    /*
     * 促销活动编辑
     */
    public function update($id)
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('Promotion');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new PromotionAlias())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose !== false) {
                win('操作成功', Url::build('Promotion/index'));
            }

            fail('操作失败');

        }

        $item = PromotionAlias::get($id);

        return $this->fetch('create', [
            "mate_title" => '促销活动编辑',
            'item'       => $item

        ]);
    }

    /*
     * 状态改变
     */
    public function auditing()
    {
        $id = Request::instance()->param('id');
        $status = PromotionAlias::where('id', $id)->value('status') == '1' ? 2 : 1;
        if (PromotionAlias::where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }

    /*
     * 排列顺序
     */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            PromotionAlias::where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('排序成功', Url::build('Promotion/index'));
    }


    /*
     * 上传组图
     */
    public function uploadArr()
    {
        $file = request()->file('file');
        if (!empty($file)) {

            // 移动到框架应用根目录/public/uploads/ 目录下
            $upload = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($upload) {

                //图片处理
                imgDispose('public' . DS . 'uploads' . DS . $upload->getSaveName());

                return json_encode(['error' => '0', 'src' => 'uploads/' . date('Ymd') . '/' . $upload->getFilename(), 'name' => $upload->getSaveName()]);
            }
            return json_encode(['error' => '失败']);
        }
    }
}
